import { Component, OnInit } from '@angular/core';
import { NavController } from '@ionic/angular';
import { HttpService } from 'src/app/service/http.service';
import { PopwinService } from 'src/app/service/popwin.service';
import { UrlService } from 'src/app/service/url.service';
import { ConfigService } from 'src/app/service/config.service';
import { ToolService } from 'src/app/service/tool.service';
import * as $ from 'jquery';

@Component({
  selector: 'app-promotion',
  templateUrl: './promotion.page.html',
  styleUrls: ['./promotion.page.scss'],
})
export class PromotionPage implements OnInit {
  public promotionData={"all":"","give":"","lottery":"","other":""};
  public stateData={"isAll":true,"isGive":false,"isLottery":false,"isOther":false};
  constructor(private navCtrl: NavController,private http:HttpService,private popWin:PopwinService) { }

  ngOnInit() {
    this.promotionNet();
  }
  backClick() {
    this.navCtrl.navigateBack("tabs/my");
  }

  changeTab(typeStr:String){
      switch(typeStr){
          case 'all':{
            this.stateData={"isAll":true,"isGive":false,"isLottery":false,"isOther":false};
             break;
          }
          case 'give':{
            this.stateData={"isAll":false,"isGive":true,"isLottery":false,"isOther":false};
            break;
          }
          case 'lottery':{
            this.stateData={"isAll":false,"isGive":false,"isLottery":true,"isOther":false};
            break;
          }
          case 'other':{
            this.stateData={"isAll":false,"isGive":false,"isLottery":false,"isOther":true};
            break;
          }
      }
  }

  promotionNet() {
    let that = this;
    let obj = "{\"type\":\"" + "promotes" + "\"}";
    this.http.get(UrlService.OTHER_CONFIG, { "callback": "JSONP_CALLBACK" , "data": ToolService.aesEncode(obj)}).subscribe(res => {
      let data = res.json();
      ToolService.setEncrypt(data.encrypt);
      if (data.code == ConfigService.SUCCESS_CODE) {
        let temp = data.data;
        let res = ToolService.aesDecodeToJson(temp);
        that.promotionData={"all":res.all.cfg_val,"give":res.give.cfg_val,"lottery":res.lottery.cfg_val,"other":res.other.cfg_val};
        
        that.showData();
        console.log(res);
      } else if (data.code == ConfigService.FAIL_CODE) {
        that.popWin.toast(data.message);
      } else if (data.code == ConfigService.LOGIN_CODE) {
        that.navCtrl.navigateForward("/login");
      }
    }, error => {
      console.log("error", error);
    });
  }

  showData(){
   let that=this;
    let timer=  setInterval(function(){
        show();
        clearInterval(timer);
     },1000);
     function show(){
      $("#pro-all").html(that.promotionData.all);
      $("#pro-give").html(that.promotionData.give);
      $("#pro-lottery").html(that.promotionData.lottery);
      $("#pro-other").html(that.promotionData.other);
     }
  }


}
